{include file="public/header"/}
<link rel="stylesheet" href="{__PUBLIC_PATH}/admin/pcf/pcfbase1.css?v={$version}"/>
<script src="{__PUBLIC_PATH}/common/js/jquery.js?v={$version}"></script>
<script src="{__PUBLIC_PATH}/admin/pcf/global.js?v={$version}"></script>
</head>
<body>
<style>
    .layui-form-selected dl{z-index: 999999 !important;}
    @media screen and (max-width:1000px){
        .pcfwidth{width:100%!important;}
    }
</style>
<div class="layui-form">
    <div class="layui-fluid" style="padding-bottom:83px;">
        <div class="layui-card">
            <div class="layui-card-body">
                <div class="layui-form-item layui-row">
                    <div class="layui-form-item layui-col-xs6 pcfwidth">
                        <label class="layui-form-label"><span style="color:red;">*</span> 字段标题</label>
                        <div class="layui-input-block">
                            <input type="text" name="title" value="{$info.title|default=''}" lay-verify="required" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item layui-col-xs6 pcfwidth">
                      <label class="layui-form-label"><span style="color:red;">*</span> 字段名称</label>
                      <div class="layui-input-block">
                          <input type="text" value="{$info.name|default=''}" {if condition="$info.ifsystem eq 1"} readonly {/if}  name="name" lay-verify="required|check_name" placeholder="只允许字母、数字和下划线的任意组合" class="layui-input" onkeyup="this.value=this.value.replace(/[^0-9a-zA-Z_]/g,'');" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^0-9a-zA-Z_]/g,''));">
                      </div>
                    </div>
                    <div class="layui-form-item">
                      <label class="layui-form-label"><span style="color:red;">*</span> 字段类型</label>
                      <div class="layui-input-inline">
                          <select name="dtype" id="dtype" lay-filter="dtype" lay-verify="check_dtype"  {if condition="$info.ifsystem eq 1"} disabled="disabled" {/if} >
                            {volist name='$fieldtype_list' id='vo'}
                            <option value="{$vo.name}" data-ifoption="{$vo.ifoption|default=0}" {eq name="$info.dtype" value="$vo.name"}selected{/eq}>{$vo.title}</option>
                            {/volist}
                          </select>
                      </div>
                    </div>
                    <div class="layui-form-item layui-col-xs6 pcfwidth none" id="dl_dfvalue">
                      <label class="layui-form-label" id="label_dfvalue">默认值</label>
                      <div class="layui-input-block">
                          <textarea rows="5" cols="60" id="dfvalue" name="dfvalue" lay-verify="check_dfvalue" class="layui-textarea" placeholder="如果定义字段类型为下拉框、单选项、多选项时，此处填写被选择的项目(用“,”分开，如“男,女,人妖”)" style="height:76px;">{$info.dfvalue|default=''}</textarea>
                      </div>
                    </div>
                    <div class="layui-form-item" id="dl_dfvalue_unit">
                      <label class="layui-form-label">数值单位</label>
                      <div class="layui-input-inline">
                          <input type="text" value="{$info.dfvalue_unit|default=''}" name="dfvalue_unit" id="dfvalue_unit" placeholder="比如：元、个、件等等" class="layui-input">
                      </div>
                    </div>
                    <div class="layui-form-item" style="display:none;">
                      <label class="layui-form-label">应用于筛选</label>
                      <div class="layui-input-inline">
                            <input type="checkbox" lay-filter="is_screening" lay-skin="switch" lay-text="是|否" {if condition='$info.is_screening == 1'}checked{/if}>
                            <input type="hidden" name="is_screening" value="{$info.is_screening|default='0'}" />
                            <input type="hidden" name="IsScreening_status" value="0">
                      </div>
                    </div>
                    <div class="layui-form-item layui-col-xs6 pcfwidth">
                      <label class="layui-form-label">提示文字</label>
                      <div class="layui-input-block">
                        <textarea name="remark" class="layui-textarea" placeholder="问号提示文字">{$info.remark|default=''}</textarea>
                      </div>
                    </div>
                    <div class="layui-form-item">
                      <label class="layui-form-label" id="select_title">{if condition='$info.is_screening == 1'}<span style="color:red;">*</span> {/if}指定栏目</label>
                      <div class="layui-input-inline">
                          <select id="typeid" lay-verify="check_typeid"  multiple="true">
                            <option value="0" {if condition="in_array(0, $typeids) OR empty($typeids)"}selected="true"{/if} {if condition='$info.is_screening == 1'} class="none" disabled{/if}>—指定所有栏目—</option>
                            {$select_html|raw}
                          </select>
                      </div>
                    </div>
                    <input type="hidden" name="channel_id" id="channel_id" value="{$info.channel_id|default=''}">
                    <input type="hidden" name="id" id="id" value="{$info.id|default=''}">
                    <input type="hidden" name="old_name" id="old_name" value="{$info.name|default=''}">
                    <input type="hidden" name="old_dtype" id="old_dtype" value="{$info.dtype|default=''}">
                </div>
            </div>
            <div style="clear:both;"></div>
        </div>
    </div>
    <div class="submit-info text-center">
        <button class="layui-btn" lay-submit lay-filter="formSubmit">确认提交</button>
        <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
    </div>
</div>
{include file="public/footer"/}
<script>
  layui.use(['layer', 'form'], function () {
    var $ = layui.$,
      layer = layui.layer,
      form = layui.form;

    dtype_change($('select[name=dtype]'));
    form.verify({
      check_name: function(value, item){
        var name = $('input[name="name"]').val();
        if (/^[0-9]+$/.test(name)) {
            return '字段名称不能纯数字！';
        } else if (/^((_+)|(\d+))$/.test(value) || !/^(\w+)$/.test(value)) {
            return '字段名称格式不正确！';
        } else if(/^type/.test(value)){
            return '字段名称不允许以type开头！';
        }
      },
      check_dtype: function(value, item){
        if ('' == value || undefined == value) {
            return '请选择字段类型！';
        }
      },
      check_dfvalue: function(value, item){
        var dtype_value = $('#dtype').find('option:selected').val();
        var ifoption = $('#dtype').find('option:selected').data('ifoption');
        if (1 == ifoption) {
            if ($.trim($('#dfvalue').val()) == '') {
                $('#dfvalue').focus();
                return '默认值不能为空！';
            }
            var tag = '|';
            if($('#dfvalue').val().indexOf(tag) != -1){
                $('#dfvalue').focus();
                return '默认值不能输入 | 符号！';
        　　}
        }
      },
      check_typeid: function(value, item){
        if (0 >= parseInt($('#typeid').find('option:selected').length)) {
            return '请选择指定栏目！';
        }
      }
    });
    // 字段类型
    form.on('select(dtype)', function(data){
        dtype_change(data.elem);
        form.render();
    });
    // 监听开关
    form.on('switch(is_screening)', function(data){
      var elemId = data.elem.attributes['lay-filter']['nodeValue'];
      if (data.elem.checked) {
        this.value = 1;
        $('#select_title').html('<span style="color:red;">*</span> 指定栏目');
        $('#typeid').find('option:first').attr('disabled', true).addClass('none');
        $('#call_tags_help').show();
      } else {
        this.value = 0;
        $('#select_title').html('指定栏目');
        $('#typeid').find('option:first').attr('disabled', false).removeClass('none');
        $('#call_tags_help').hide();
      }
      $("input[name='"+elemId+"']").val(this.value);
    });
    //监听提交
    form.on('submit(formSubmit)', function(data){
        layer.load(2);
        data.field.typeids = $('#typeid').val();
        $.ajax({
            type : 'post',
            url : "{:url('/channel.field/channel_edit')}",
            data : data.field,
            dataType : 'json',
            success : function(res){
              layer.closeAll('loading');
              if(res.status){
                  layer.msg(res.msg, {icon: 1, time: 1000}, function(){
                      parent.window.location.href = res.url;
                  });
              }else{
                  layer.msg(res.msg, {icon: 2, time: 1000});
              }
            },
            error: function(e){
              layer.closeAll('loading');
              layer.msg("未知错误，操作中断！", {icon: 2, time: 1000});
            }
        });
        return false;
    });
    function dtype_change(obj) {
        var dtype = $(obj).val();
        var ifoption = $(obj).find('option:selected').data('ifoption');
        $('#dl_dfvalue').hide();
        if (0 <= $.inArray(dtype, ['datetime','switch','img','imgs','files'])) {
           $('#dl_dfvalue').hide();
        } else {
           $('#dl_dfvalue').show();
        }
        if (1 == ifoption) {
            $('#label_dfvalue').html('<span style="color:red;">*</span> 默认值');
        } else {
            $('#label_dfvalue').html('默认值');
        }
        if (0 <= $.inArray(dtype, ['checkbox','radio','select','int','float','decimal'])) {
            $('#IsScreening').show();
            $('input[name=IsScreening_status]').val(1);
        } else {
            $('#IsScreening').hide();
            $('input[name=IsScreening_status]').val(0);
            $('#select_title').html('指定栏目');
        }
        if (0 <= $.inArray(dtype, ['text','int','float','decimal'])) {
            $('#dl_dfvalue_unit').show();
        } else {
            $('#dl_dfvalue_unit').hide();
        }
    }

  });
</script>
